//
//  WBWelcomeView.swift
//  WeiBoApp
//
//  Created by James on 2017/7/13.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit
import SDWebImage

/// 欢迎试图
class WBWelcomeView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    @IBOutlet weak var iconView: UIImageView!
    
    @IBOutlet weak var nameLabel: UILabel!
    
    @IBOutlet weak var bottumConstrain: NSLayoutConstraint!
    
    class func welcomeView()->WBWelcomeView {
        let nib = UINib(nibName: "WBWelcomeView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0]
        
        return v as! WBWelcomeView
    }
    
    override func awakeFromNib() {
        guard let urlStr = WBNetWorkManger.shared.userAcount.avatar_large,
            let url = URL(string:urlStr)  else{
            return
        }
        
        //设置头像
        iconView.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "avatar_default_big"))
        
        let path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 85, height: 85))
        
        let layer = CAShapeLayer()
        
        layer.path = path.cgPath
        
        iconView.layer.mask = layer
    }
    
    ///自动布局系统更新完成约束后，会自动调用这个方法
    ///通常对子试图布局进行修改
    override func layoutSubviews() {
        
    }
    
    ///试图被添加到 Window上 ，表示试图已经显示
    
    override func didMoveToWindow() {
        self.layoutIfNeeded()
        UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations:{
            
            self.bottumConstrain.constant = self.bounds.size.height - 200
            
            ///更新约束
            self.layoutIfNeeded()
            
            
        }){(_) in
           
            UIView.animate(withDuration: 1.0, animations: {
                self.nameLabel.alpha = 1

            }){ (_)in
                self.removeFromSuperview()
            }
        }
    }
}
